﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TravelServer.Dtos;
using TravelServer.DTOs;
using TravelServer.Models;
using TravelServer.Services;

namespace TravelServer.Controllers
{
    [Route("api/touristRoutes/{touristRouteId}/pictures")]
    [ApiController]
    public class TouristRoutePicturesController : ControllerBase
    {
        private readonly ITouristRoutsRepository routersRepository;
        private readonly IMapper mapper;

        public TouristRoutePicturesController(ITouristRoutsRepository routersRepository, IMapper mapper)
        {
            this.routersRepository = routersRepository;
            this.mapper = mapper;
        }

        [HttpGet]
        public async Task<IActionResult> GetPictureListForTouristRoute(Guid touristRouteId)
        {
            if (!await routersRepository.HasTouristRouteAsync(touristRouteId))
            {
                return NotFound("旅游线路不存在！");
            }

            var picturesFromRepo =await routersRepository.GetTouristRoutePicturesByTouristRouteIdAsync(touristRouteId);
            if (picturesFromRepo == null || picturesFromRepo.Count() <= 0) { return NotFound("旅游线路不存在！"); }
            return Ok(mapper.Map<IEnumerable<TouristRoutePicturesDto>>(picturesFromRepo));
        }

        //Name 用来定义URL的名称，提供给下面POST请求返回201结果 URL
        [HttpGet("{pictureId}",Name = "GetPictureById")]
        public async Task<IActionResult> GetPictureById(Guid touristRouteId, int pictureId)
        {
            if (! await routersRepository.HasTouristRouteAsync(touristRouteId))
            {
                return NotFound("旅游线路不存在！");
            }
            var pictureFromRepo = await routersRepository.GetTouristRoutePicturesByPicturesIdAsync(pictureId);
            if (pictureFromRepo == null)
            {
                return NotFound("旅游线路不存在！");
            }
            return Ok(mapper.Map<TouristRoutePictures>(pictureFromRepo));
        }

        [HttpPost]
        public async Task<IActionResult> ADDTouristRoutePictures([FromRoute] Guid touristRouteId, [FromBody] TouristRoutePicturesCreationDto touristRoutePicturesCreationDto)
        {
            if (!await routersRepository.HasTouristRouteAsync(touristRouteId))
            {
                return NotFound("旅游线路不存在！");
            }
            var pictureFromRepo = mapper.Map<TouristRoutePictures>(touristRoutePicturesCreationDto);
            routersRepository.ADDTouristRoutePicture(touristRouteId, pictureFromRepo);
            //post请求需要返回
            var res =await routersRepository.SaveDataAsync();
            if (res)
            {
                var touristRoutPictureDto = mapper.Map<TouristRoutePicturesDto>(pictureFromRepo);
                //post成功 返回201 CreatedAtRoute  类似return ok的用法
                var postResult = CreatedAtRoute(
                    "GetPictureById", //返回URL，通过id可以查询到保存的结果,该地址保存在Header里的Location
                    new
                    {
                        touristRouteId = pictureFromRepo.TouristRouteId,
                        pictureId = pictureFromRepo.Id
                    },//URL参数对象,对应(GetPictureById)接口的2个参数
                    touristRoutPictureDto);//返回的值

                return postResult;
            }
            return BadRequest();

        }

        [HttpDelete("{pictureId}")]
        public async Task<IActionResult> DeleteTouristRoutePicture([FromRoute] Guid touristRouteId, [FromRoute]int pictureId) 
        {
            if (!await routersRepository.HasTouristRouteAsync(touristRouteId))
            {
                return NotFound("旅游线路不存在！");
            }
            var picture=await routersRepository.GetTouristRoutePicturesByPicturesIdAsync(pictureId);

            routersRepository.DeleteTouristRoutePicture(picture);
           await routersRepository.SaveDataAsync();
            return NoContent();
        }
    }
}
